package main;

import java.rmi.RemoteException;

import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import data.DataObject;

import topic.Topic;
import topic.impl.TopicImpl;

import embeded.impl.EmbededObjectImpl;

public class main_embeded_client {

	ORB orb = null;
	POA poa = null;

	public main_embeded_client(String[] args) {
		orb = ORB.init(new String[]{}, null);
		new Thread(new Runnable() {
			public void run() {
				orb.run();
			}
		}).start();
		try {
			poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			poa.the_POAManager().activate();

		} catch (Exception e) {
			System.out
					.println("EmbededObjectImpl : erreur d'activation du poa !!!");
			return;
		}
		
		try {
			EmbededObjectImpl embeded = new EmbededObjectImpl(args[0], orb, poa);
			embeded.broadcast();
			
			DataObject data = new DataObject(orb.create_any());
			Topic t1 = new TopicImpl("com", "root", poa, data);
			t1.getDataObject().setString("ibiou");

			embeded.addTopic("root", t1);
			embeded.addTopic("root", new TopicImpl("fr", "root", poa, new DataObject()));
			embeded.addTopic("root.com", new TopicImpl("com1", "root.com", poa, new DataObject()));
			embeded.addTopic("root.com", new TopicImpl("com2", "root.com", poa, new DataObject()));
			embeded.addTopic("root.fr", new TopicImpl("fr1", "root.fr", poa, new DataObject()));
			embeded.addTopic("root.fr.fr1", new TopicImpl("fr2", "root.fr.fr1", poa, new DataObject()));
			embeded.addTopic("root.fr.fr1.fr2", new TopicImpl("fr3", "root.fr.fr1.fr2", poa, new DataObject()));
			embeded.addTopic("root.fr.fr1.fr2", new TopicImpl("fr5", "root.fr.fr1.fr2", poa, new DataObject()));
			embeded.addTopic("root.fr.fr1.fr2.fr3", new TopicImpl("fr4", "root.fr.fr1.fr2.fr3", poa, new DataObject()));
			embeded.showFrame();
			
		/*	try {Thread.sleep(1000);} catch (InterruptedException e) {}
			DataObject data1 = new DataObject();
			data1.setString("bonjour");
			embeded.setData("root.fr.fr1.fr2.fr3", data1);
			embeded.showFrame();
			System.out.println(embeded.getData("root.fr.fr1.fr2.fr3").getString());
			
			try {Thread.sleep(1000);} catch (InterruptedException e) {}
			embeded.removeTopic("root.fr.fr1.fr2.fr5");
			
			try {Thread.sleep(1000);} catch (InterruptedException e) {}
			embeded.addTopic("root.fr.fr1.fr2", new TopicImpl("fr5", "root.fr.fr1.fr2", poa, new DataObject()));
			*/
		} catch (RemoteException e) {
			System.out.println("Main");
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new main_embeded_client(args);
	}

}
